﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using FrameWork.Lib;
using FrameWork.Lib.DataObjects;

namespace FrameWork.Lib.Diagnostics
{
    public class Functions
    {
        public static Result ExecuteBatch(string filename, bool waitforexit)
        {
            var processor = new ExecuteBatchProcessor();

            return processor.ExecuteBatch(filename, waitforexit);
        }


        private class ExecuteBatchProcessor
        {

            public Result LocalResult { get; set; }
            public Process LocalProcess { get; set; }

            public Result ExecuteBatch(string filename, bool waitforexit)
            {
                LocalResult = new Result();


                LocalProcess = new Process();
                try
                {
                    LocalProcess.StartInfo.CreateNoWindow = true;
                    LocalProcess.StartInfo.ErrorDialog = false;
                    LocalProcess.StartInfo.UseShellExecute = false;
                    LocalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    LocalProcess.StartInfo.FileName = filename;

                    LocalProcess.StartInfo.RedirectStandardError = true;
                    LocalProcess.StartInfo.RedirectStandardOutput = true;

                    LocalProcess.StartInfo.StandardErrorEncoding = Encoding.Default;
                    LocalProcess.StartInfo.StandardOutputEncoding = Encoding.Default;

                    LocalProcess.Start();

                    if (waitforexit)
                    {
             

                        LocalProcess.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
                        LocalProcess.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
                        LocalProcess.BeginErrorReadLine();
                        LocalProcess.BeginOutputReadLine();
                        LocalProcess.WaitForExit();

                        //LocalProcess.CancelErrorRead();
                        //LocalProcess.CancelOutputRead();
                    }
                }
                catch (Exception exc)
                {
                    if (LocalProcess != null)
                    {
                        LocalProcess.Kill();
                    }

                    LocalResult.AddResult(exc);
                }

                if (standarderrorBuilder.ToString().Trim().Length > 0)
                    LocalResult.AddResult(standarderrorBuilder.ToString(), ResultSeverityType.Error);
                if (standardoutputBuilder.ToString().Trim().Length > 0)
                    LocalResult.AddResult(standardoutputBuilder.ToString(), ResultSeverityType.Info);

                return LocalResult;
            }

            private StringBuilder standarderrorBuilder = new StringBuilder();

            private void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
            {

                standarderrorBuilder.Append(e.Data);


            }

            private StringBuilder standardoutputBuilder = new StringBuilder();

            private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {

                standardoutputBuilder.Append(e.Data);

            }
        }

    }
}
